package com.jade.journal;

import com.jade.framework.base.util.DataUtils;

/**
 * 日志级别。
 *
 * @author Daisy
 * @version 1.0 14-9-21 下午8:40
 */
public enum JournalLevel
{
    STAT("stat"),
    INFO("info"),
    NOTICE("warn"),
    WARN("notice"),
    ERROR("error"),
    FATAL("fatal");

    private static final JournalLevel[] LEVELS
            = {STAT, INFO, WARN, NOTICE, ERROR, FATAL};

    public static final JournalLevel toLevel(int level)
    {
        return toLevel(level, INFO);
    }

    private String name;


    /**
     * 构造
     *
     * @param name  名称
     */
    private JournalLevel(String name)
    {
        this.name = name;
    }


    public String getName()
    {
        return name;
    }

    public int getIndex()
    {
        return ordinal();
    }


    /**
     * Convert the string passed as argument to a level. If the
     * conversion fails, then this method returns {@link #INFO}.
     */
    public static JournalLevel toLevel(String sArg)
    {
        return toLevel(sArg, JournalLevel.INFO);
    }

    /**
     * Convert an integer passed as argument to a level. If the
     * conversion fails, then this method returns the specified default.
     */
    public static JournalLevel toLevel(int level, JournalLevel defaultLevel)
    {
        if (level < 6 && level >= 0) {
            return LEVELS[level];
        }
        return defaultLevel;
    }

    /**
     * Convert the string passed as argument to a level. If the
     * conversion fails, then this method returns the value of
     * <code>defaultLevel</code>.
     */
    public static JournalLevel toLevel(String s, JournalLevel defaultLevel)
    {
        if (s == null) {
            return defaultLevel;
        }

        if ("stat".equalsIgnoreCase(s)) {
            return STAT;
        }
        else if ("info".equalsIgnoreCase(s)) {
            return INFO;
        }
        else if ("warn".equalsIgnoreCase(s)) {
            return WARN;
        }
        else if ("notice".equalsIgnoreCase(s)) {
            return NOTICE;
        }
        else if ("error".equalsIgnoreCase(s)) {
            return ERROR;
        }
        else if ("fatal".equalsIgnoreCase(s)) {
            return FATAL;
        }

        int l = DataUtils.getInt(s, 1);
        return toLevel(l);
    }
}